package com.itheima.youdianda.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.BaseContext;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.dto.YddArticleDto;
import com.itheima.youdianda.mapper.YddArticleMapper;
import com.itheima.youdianda.pojo.YddArticle;
import com.itheima.youdianda.pojo.YddCate;
import com.itheima.youdianda.service.YddArticleService;
import com.itheima.youdianda.service.YddCateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;


@Service
@Slf4j
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper, YddArticle> implements YddArticleService {
    @Autowired
    private YddCateService yddCateService;

    /**
     * 文章列表-分页
     *
     * @param start_time
     * @param end_time
     * @param cateid
     * @param title
     * @param page1
     * @param pageSize
     * @return
     */
    @Override
    public R getPage(String start_time, String end_time, String cateid, String title, Long page1, Long pageSize) {
        //分页查询
        Page<YddArticle> ipage = new Page<>(page1, pageSize);
        //条件查询:
        //创建构造器
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        //设置标题
        wrapper.like(StrUtil.isNotEmpty(title), YddArticle::getTitle, title);
        //设置分类id
        wrapper.eq(StrUtil.isNotEmpty(cateid), YddArticle::getCateid, cateid);
        //设置时间条件
        wrapper.between(StrUtil.isNotEmpty(start_time) && StrUtil.isNotEmpty(end_time), YddArticle::getCreateDate, Convert.toLong(start_time), Convert.toLong(end_time));
//          wrapper.between(StrUtil.isNotEmpty(startTime)||StrUtil.isNotEmpty(endTime),YddArticle::getUpdateDate,startTime,endTime);
        //排序
        wrapper.orderByDesc(YddArticle::getCreateDate);
        ipage = page(ipage, wrapper);
        //查询catename
        ipage.getRecords().stream().map(item->{
            YddCate yddCate = yddCateService.getById(item.getCateid());
            item.setCatename(yddCate.getCatename());
//            item.setCreate_date(Convert.toInt(item.getCreateDate()));
            return item;
        }).collect(Collectors.toList());
        //将返回值封装
        YddArticleDto yddArticleDto = new YddArticleDto();
        yddArticleDto.setData(ipage.getRecords());
        yddArticleDto.setCurrentPage((int) ipage.getCurrent());
        yddArticleDto.setPageSize((int) ipage.getSize());
        yddArticleDto.setCount((int) ipage.getTotal());
        yddArticleDto.setTotalPages((int) ipage.getPages());
        return R.success(yddArticleDto);

    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @Override
    public R deleteYddarticle(Integer id) {
        removeById(id);
        log.info("删除成功");
        return R.success("");

    }

    /**
     * 查看详情(数据回显)
     *
     * @param id
     * @return
     */
    @Override
    public R getYddarticleById(Integer id) {
        log.info("回显数据id:{}", id);
        return R.success(getById(id));
    }

    /**
     * 修改文章
     *
     * @param yddArticle
     * @return
     */
    @Override
    public R updateYddarticle(YddArticle yddArticle) {
        log.info("修改id", yddArticle.getId());
        if (ObjectUtil.isNotNull(yddArticle)) {
            updateById(yddArticle);
            return R.success("修改成功");
        } else
            return R.error("");
    }

    /**
     * 添加文章
     *
     * @param yddArticle
     * @return
     */
    @Override
    public R addYddarticle(YddArticle yddArticle) {
        yddArticle.setUserId(Convert.toInt(BaseContext.getId()));
        save(yddArticle);
        return R.success(null);
    }


}
